<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>用户查询</title>
    <link rel="shortcut icon" href="/img/favicon.ico" /><!--标签页面顶部的小图标-->
    <script th:src="@{/webjars/jquery/3.3.1/jquery.js}"></script>
    <link th:href="@{/webjars/bootstrap/4.0.0/css/bootstrap.css}" rel="stylesheet"/>
    <script th:src="@{/webjars/bootstrap/4.0.0/js/bootstrap.js}"></script>
    <script>

        function updateUserStateByUName(uName,uShenHe) {
            $.ajax({
                url:"/admin/updateUserStateByUName.do",
                type:"post",
                data:{uName:uName,uShenHe:uShenHe},
                success:function (result) {
                    findUser(null,null,null,null,null);
                },
                error:function () {
                    alert("系统异常");
                }
            })
        }

        function findUser(uName,uDiZhi,uPhone,usName,uShenHe) {
            $.ajax({
                url:"/admin/findUser.do",
                type:"get",
                data:{uName:uName,uDiZhi:uDiZhi,uPhone:uPhone,usName:usName,uQuanXian:2,uShenHe:uShenHe},
                success:function (result) {
                    $("#tbody").html("");
                    var count = 0;
                    for (var i=0;i<result.length;i++) {
                        var user;
                        if (uShenHe == '1') {
                            if (result[i].uShenHe == 1 && result[i].uQuanXian == 2) {
                                user = result[i];
                                var state = user.uShenHe == 1 ? "<button class='btn btn-sm btn-warning' onclick='updateUserStateByUName(\"" + user.uName + "\",3)'>冻结</button>"
                                    : "<button class='btn btn-sm btn-primary' onclick='updateUserStateByUName(\"" + user.uName + "\",1)'>解冻</button>"
                                $("#tbody").append("<tr>" +
                                    "<td>" + (++count) + "</td>" +
                                    "<td>" + user.uName + "</td>" +
                                    "<td>" + user.uSex + "</td>" +
                                    "<td>" + user.uAge + "</td>" +
                                    "<td>" + user.uDiZhi + "</td>" +
                                    "<td>" + user.uPhone + "</td>" +
                                    "<td>" + user.uEmail + "</td>" +
                                    "<td>" + user.usName + "</td>" +
                                    "<td>" + state +
                                    "&nbsp;&nbsp;<button class='btn btn-danger btn-sm' value='"+user.uName+"' onclick='deleteUserByUName(this)'>删除</button> " +
                                    "</td>" +
                                    "</tr>")
                            }
                        }else if (uShenHe=='3') {
                            if (result[i].uShenHe == 3 && result[i].uQuanXian == 2) {
                                user = result[i];
                                var state = user.uShenHe == 1 ? "<button class='btn btn-sm btn-warning' onclick='updateUserStateByUName(\"" + user.uName + "\",3)'>冻结</button>"
                                    : "<button class='btn btn-sm btn-primary' onclick='updateUserStateByUName(\"" + user.uName + "\",1)'>解冻</button>"
                                $("#tbody").append("<tr>" +
                                    "<td>" + (++count) + "</td>" +
                                    "<td>" + user.uName + "</td>" +
                                    "<td>" + user.uSex + "</td>" +
                                    "<td>" + user.uAge + "</td>" +
                                    "<td>" + user.uDiZhi + "</td>" +
                                    "<td>" + user.uPhone + "</td>" +
                                    "<td>" + user.uEmail + "</td>" +
                                    "<td>" + user.usName + "</td>" +
                                    "<td>" + state +
                                    "&nbsp;&nbsp;<button class='btn btn-danger btn-sm' value='"+user.uName+"' onclick='deleteUserByUName(this)'>删除</button> " +
                                    "</td>" +
                                    "</tr>")
                            }
                        }else {
                            if (result[i].uQuanXian == 2) {
                                user = result[i];
                                var state = user.uShenHe == 1 ? "<button class='btn btn-sm btn-warning' onclick='updateUserStateByUName(\"" + user.uName + "\",3)'>冻结</button>"
                                    : "<button class='btn btn-sm btn-primary' onclick='updateUserStateByUName(\"" + user.uName + "\",1)'>解冻</button>"
                                $("#tbody").append("<tr>" +
                                    "<td>" + (++count) + "</td>" +
                                    "<td>" + user.uName + "</td>" +
                                    "<td>" + user.uSex + "</td>" +
                                    "<td>" + user.uAge + "</td>" +
                                    "<td>" + user.uDiZhi + "</td>" +
                                    "<td>" + user.uPhone + "</td>" +
                                    "<td>" + user.uEmail + "</td>" +
                                    "<td>" + user.usName + "</td>" +
                                    "<td>" + state +
                                    "&nbsp;&nbsp;<button class='btn btn-danger btn-sm' value='"+user.uName+"' onclick='deleteUserByUName(this)'>删除</button> " +
                                    "</td>" +
                                    "</tr>")
                            }
                        }
                    }
                }
            })
        }

        function deleteUserByUName(uName){
            if (confirm("确定要删除此用户吗？")){
                $.ajax({
                    url:"/admin/deleteUserByUName.do",
                    type:"post",
                    data:{uName:$(uName).val()},
                    success:function (result) {
                        alert("删除成功！");
                        $(uName).parent("td").parent("tr").hide();
                    },
                    error:function () {
                        alert("系统异常！");
                    }
                })
            }
        }

        $(function () {
            findUser(null,null,null,null,null);
        })

        function find(){
            findUser($("#findByUName").val(),$("#findByUDiZhi").val(), $("#findByUPhone").val(),
                $("#findByUsName").val(),$("#findByUShenHe").val());
            $("#findByUName").val("");
            $("#findByUDiZhi").val("");
            $("#findByUPhone").val("");
            $("#findByUsName").val("");
            $("#findByUShenHe").val("");
        }
    </script>
</head>

</head>
<body>
    <div id="allText" style="margin-left: 10px;margin-top: 30px">
        <div style="float: left;width: 5%;height: 70px"></div>
        <div style="float: left;margin-right: 25px"><input type="text" id="findByUName" placeholder="用户名"></div>
        <div style="float: left;margin-right: 25px"><input type="text" id="findByUDiZhi" placeholder="地址"></div>
        <div style="float: left;margin-right: 25px"><input type="text" id="findByUPhone" placeholder="电话"></div>
        <div style="float: left"><input type="text" id="findByUsName" placeholder="学生姓名"></div>
        <div style="float: left;margin-right: 25px">状态:<select id="findByUShenHe">
            <option value="">全部</option>
            <option value="1">未冻结</option>
            <option value="3">已冻结</option>
        </select></div>
        <div style="float: left;margin-left: 20px"><button onclick="find()" class="btn btn-sm" style="background-color: #5cb85c">查询</button></div>
        <div>
            <table class='table table-striped' border='1' bordercolor='green'>
                <thead>
                    <tr>
                        <td>序号</td>
                        <td>用户名</td>
                        <td>性别</td>
                        <td>年龄</td>
                        <td>地址</td>
                        <td>电话</td>
                        <td>邮箱</td>
                        <td>学生姓名</td>
                        <td>操作</td>
                    </tr>
                </thead>
                <tbody id="tbody"></tbody>
            </table>
        </div>
    </div>
</body>
</html>